var TicketStatus = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            reload_after_add: true,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                {display:'Status', name:'name', width:200, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'empty', defval:''}},
                {display:'Set Closed', name:'set_end', width:300, sortable:false, align:'left', hide:false}
            ],
            formFields:[
                {type:'buttom', name:'New', bclass:'add', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Edit', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Delete', bclass:'delete', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Set As Closed', bclass:'set', onpress:self.handleGridEvent},
                {separator:true}
            ],
            searchitems:[
             {display:'Status', name:'name', isdefault:true}
             ],
            checkboxSelection:true,
            editablegrid:{
                use:true,
                url:$('#table-editable-url').val()
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'New') {
            TicketStatus.objGrid.flexBeginAdd();
        }
        else if (com == 'Edit') {
            if (FlexObject.rowSelectedCheck(TicketStatus.objGrid,grid)) {
                var row = FlexObject.getSelectedRows(grid);
                TicketStatus.objGrid.flexBeginEdit(row[0]);
            }
        }
        else if (com == 'Save') {
            TicketStatus.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            TicketStatus.objGrid.flexCancel();
        }
        else if (com == 'Delete') {
            if (FlexObject.rowSelectedCheck(TicketStatus.objGrid,grid)) {
                var url = $('#table_delete_url').val();
                TicketStatus.delete_(TicketStatus.objGrid,grid,url);
            }
        }
        else if (com == 'Set As Closed') {
            if (FlexObject.rowSelectedCheck(TicketStatus.objGrid,grid)) {
                var url = $('#table_set_end_url').val();
                //TicketStatus.delete_(TicketStatus.objGrid,grid,url);

                var row_id = FlexObject.getSelectedRowIds(grid);

                jConfirm('Are you sure you want to continue ?', 'Confirm', function(confirmation) {
                    if(confirmation){
                        $.post(url, {'id': row_id[0]},function(response){
                            //delete was successful, remove checkbox rows
                            if(response.code == 0){
                                TicketStatus.objGrid.flexReload();
                                //TicketStatus.objGrid.flexNotify('Closed Status',response.msg,'success');
                            }
                            else if(response.code == 1){
                                TicketStatus.objGrid.flexNotify('Closed Status',response.msg,'error');
                            }
                        },"json");
                    }
                });
            }
        }
    },

    delete_:function (griObject,grid_dom,url) {
        var self = this;
       Jlib.do_delete(griObject,url, grid_dom);
    }
};

/* when the page is loaded */
$(document).ready(function () {
    TicketStatus.init();
});